package com.hcinfos.gsoo.adapter;

import java.math.BigDecimal;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.poisearch.Groupbuy;
import com.amap.api.services.poisearch.PoiSearch;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.GroupByInfo;
import com.hcinfos.gsoo.util.BitmapHelp;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;

/**
 * 首页推荐
 * 
 * @author xi
 * 
 */
public class RecommendAdapter extends BaseAdapter {

	private List<GroupByInfo> list;
	private LayoutInflater mInflater;
	public int index = 0;
	public Context context;
	public boolean isVip = false;
	private PoiSearch poiSearch;

	private BitmapDisplayConfig bigPicDisplayConfig;
	private BitmapUtils bitmapUtils;

	public RecommendAdapter(Context context, List<GroupByInfo> list) {
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.tc_shop_default_logo));
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
	}

	@Override
	public int getCount() {
		return list.size() > 5 ? 5 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final RecommendHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.recommend_layout, null);
			holder = new RecommendHolder();
			holder.icon_img = (ImageView) convertView
					.findViewById(R.id.imageView);
			holder.nameTv = (TextView) convertView.findViewById(R.id.name);
			holder.distance_tv = (TextView) convertView
					.findViewById(R.id.distance_tv);
			holder.price_tv = (TextView) convertView.findViewById(R.id.price);
			holder.des_tv = (TextView) convertView.findViewById(R.id.describle);
			holder.discount_price = (TextView) convertView
					.findViewById(R.id.discount_price);
			convertView.setTag(holder);
		} else {
			holder = (RecommendHolder) convertView.getTag();
		}
		GroupByInfo groupInfo = list.get(position);
		holder.nameTv.setText(groupInfo.getShopName());

		int distance = groupInfo.getDistance();
		if (distance > 0) {
			if (distance < 1000) {
				holder.distance_tv.setText(distance + "m");
			} else {
				double d = groupInfo.getDistance() / 1000d;
				BigDecimal df = new BigDecimal(d);
				double dis = df.setScale(2, BigDecimal.ROUND_HALF_UP)
						.doubleValue();
				holder.distance_tv.setText(dis + " km");
			}
		} else {
			holder.distance_tv.setVisibility(View.GONE);
		}
		if (groupInfo.getList() != null && groupInfo.getList().size() > 0) {
			Groupbuy buy = groupInfo.getList().get(0);
			holder.price_tv.setText(buy.getGroupbuyPrice() + "元");
			holder.des_tv.setText(buy.getDetail() == null ? "暂无" : buy
					.getDetail());
			holder.discount_price.getPaint().setFlags(
					Paint.STRIKE_THRU_TEXT_FLAG);
			holder.discount_price.setText(buy.getOriginalPrice() + "元");
			if (buy.getPhotos() != null && buy.getPhotos().size() > 0) {
				String url = buy.getPhotos().get(0).getUrl();
				bitmapUtils.display(holder.icon_img, url, bigPicDisplayConfig);
				// ImageUtil.setThumbnailView(buy.getPhotos().get(0).getUrl(),
				// holder.icon_img, context, new ImageCallback() {
				// @Override
				// public void loadImage(Bitmap bitmap,
				// String imagePath) {
				// holder.icon_img.setImageBitmap(bitmap);
				// }
				// }, true, false);
				// } else {
				// holder.icon_img.setImageResource(R.drawable.test_2);
			}
		} else {

		}
		return convertView;
	}

	private final class RecommendHolder {
		TextView nameTv;
		ImageView icon_img;
		TextView price_tv;
		TextView des_tv;
		TextView distance_tv;
		TextView discount_price;

	}

	public void setDataChange(List<GroupByInfo> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void setPoiSearch(PoiSearch poiSearch) {
		this.poiSearch = poiSearch;
	}

}
